package com.lyw.mall.gateway.security;

import com.alibaba.fastjson.JSONObject;
import com.lyw.mall.common.api.ObjectRestResponse;
import com.lyw.mall.gateway.entity.LoginUser;
import com.lyw.mall.gateway.utils.JwtTokenUtil;
import com.lyw.mall.gateway.utils.JwtTokenUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ShiLei
 * @version 1.0.0
 * @date 2021/3/11 15:00
 * @description 登录成功处理
 */
//@Component
public class DefaultAuthenticationSuccessHandler implements ServerAuthenticationSuccessHandler {

  /**
   * token 过期时间
   */
  @Value("${jwt.token.expired}")
  private int jwtTokenExpired;

  /**
   * 刷新token 时间
   */
  @Value("${jwt.token.refresh.expired}")
  private int jwtTokenRefreshExpired;

  @Override
  public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
    JwtTokenUtils jwtTokenUtils = new JwtTokenUtils();

    return Mono.defer(() -> Mono.just(webFilterExchange.getExchange().getResponse()).flatMap(response -> {
      DataBufferFactory dataBufferFactory = response.bufferFactory();
      // 生成JWT token
      Map<String, Object> map = new HashMap<>(2);
      LoginUser userDetails = (LoginUser) authentication.getPrincipal();
      map.put("userId", userDetails.getUserId());
      map.put("username", userDetails.getUsername());
      map.put("roles",userDetails.getAuthorities());
      String token = jwtTokenUtils.generateToken(map, userDetails.getUsername(), jwtTokenExpired);
      Map<String, Object> tokenMap = new HashMap<>(2);
      tokenMap.put("token", token);
      DataBuffer dataBuffer = dataBufferFactory.wrap(JSONObject.toJSONString(ObjectRestResponse.ok(tokenMap)).getBytes());
      return response.writeWith(Mono.just(dataBuffer));
    }));
  }
}